using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmartBed : MonoBehaviour
{
    public DynamicMeshGenerator dynamicMeshGenerator;

    void Start()
    {
        // 启动测试数据协程
        StartCoroutine(TestBedData());
    }

    IEnumerator TestBedData()
    {
        yield return new WaitForSeconds(1);

        // 测试数据索引
        int testIndex = 0;
        
        while (true)
        {
            yield return new WaitForSeconds(1);
            
            // 创建测试数据
            SmartBedData data = CreateTestData(testIndex);
            
            // 解析数据
            data.ParseData();
            
            // 处理数据
            OnReceiveBedData(data);
            
            // 更新测试索引，循环使用不同的测试数据
            testIndex = (testIndex + 1) % 5;
        }
    }
    
    /// <summary>
    /// 创建测试数据
    /// </summary>
    /// <param name="testIndex">测试数据索引</param>
    /// <returns>测试数据对象</returns>
    private SmartBedData CreateTestData(int testIndex)
    {
        SmartBedData data = new SmartBedData();
        
        // 设置基本数据
        data.time = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        data.side = "left";
        data.posture = "yes";
        data.airbag_status = "close";
        data.avg_pressure = "50";
        data.PI = "75";
        data.com_level = "85";
        data.data_status = "normal";
        
        // 根据测试索引生成不同的压力数据
        int[] pressureData = new int[40];
        int[] plData = new int[40];
        
        switch (testIndex)
        {
            case 0: // 平躺状态
                for (int i = 0; i < 40; i++)
                {
                    pressureData[i] = Random.Range(20, 40);
                    plData[i] = Random.Range(1, 3);
                }
                break;
            case 1: // 左侧倾斜
                for (int i = 0; i < 40; i++)
                {
                    if (i < 20)
                    {
                        pressureData[i] = Random.Range(40, 60);
                        plData[i] = Random.Range(3, 5);
                    }
                    else
                    {
                        pressureData[i] = Random.Range(10, 25);
                        plData[i] = Random.Range(1, 2);
                    }
                }
                break;
            case 2: // 右侧倾斜
                for (int i = 0; i < 40; i++)
                {
                    if (i >= 20)
                    {
                        pressureData[i] = Random.Range(40, 60);
                        plData[i] = Random.Range(3, 5);
                    }
                    else
                    {
                        pressureData[i] = Random.Range(10, 25);
                        plData[i] = Random.Range(1, 2);
                    }
                }
                break;
            case 3: // 头部抬高
                for (int i = 0; i < 40; i++)
                {
                    if (i < 10 || i >= 30)
                    {
                        pressureData[i] = Random.Range(15, 30);
                        plData[i] = Random.Range(1, 2);
                    }
                    else
                    {
                        pressureData[i] = Random.Range(35, 55);
                        plData[i] = Random.Range(3, 4);
                    }
                }
                break;
            case 4: // 腿部抬高
                for (int i = 0; i < 40; i++)
                {
                    if (i >= 10 && i < 30)
                    {
                        pressureData[i] = Random.Range(15, 30);
                        plData[i] = Random.Range(1, 2);
                    }
                    else
                    {
                        pressureData[i] = Random.Range(35, 55);
                        plData[i] = Random.Range(3, 4);
                    }
                }
                break;
        }
        
        // 将数组转换为字符串格式
        data.pressure = "[" + string.Join(", ", pressureData) + "]";
        data.PL = "[" + string.Join(", ", plData) + "]";
        
        // 生成其他数组数据
        float[] paiData = new float[40];
        int[] airbagTimeData = new int[40];
        for (int i = 0; i < 40; i++)
        {
            paiData[i] = pressureData[i] * 0.8f + Random.Range(-5f, 5f);
            airbagTimeData[i] = 0;
        }
        
        data.PAI = "[" + string.Join(", ", paiData) + "]";
        data.airbag_time = "[" + string.Join(", ", airbagTimeData) + "]";
        
        return data;
    }


    public void OnReceiveBedData(SmartBedData data)
    {
        // 检查数据是否有效
        if (data == null || data.plArray == null || data.plArray.Length == 0)
        {
            Debug.LogWarning("接收到的床数据无效");
            return;
        }
        
        // 检查DynamicMeshGenerator是否已设置
        if (dynamicMeshGenerator == null)
        {
            Debug.LogWarning("DynamicMeshGenerator未设置");
            return;
        }
        
        // 更新网格数据
        dynamicMeshGenerator.UpdateYOffsetData(data.plArray);
        
        // 输出调试信息
        Debug.Log($"更新床数据 - 时间: {data.time}, 姿势: {data.posture}, 数据点数: {data.plArray.Length}");
    }
}
